package com.itheima.bos.web.action;

import javax.ws.rs.core.MediaType;

import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import com.itheima.bos.domain.base.Area;
import com.itheima.bos.domain.constant.Constant;
import com.itheima.bos.domain.take_delivery.Order;
import com.itheima.bos.web.common.BaseAction;
import com.itheima.crm.domain.Customer;

public class OrderAction extends BaseAction<Order> {

	// 寄件人省市区
	private String sendAreaInfo;
	// 收件人省市区
	private String recAreaInfo;

	public void setSendAreaInfo(String sendAreaInfo) {
		this.sendAreaInfo = sendAreaInfo;
	}

	public void setRecAreaInfo(String recAreaInfo) {
		this.recAreaInfo = recAreaInfo;
	}

	@Action(value = "order_add", results = { @Result(name = "success", type = "redirect", location = "index.html") })
	public String add() {
		// 手动封装关联Area
		String[] sendAreaArray = sendAreaInfo.split("/");
		Area sendArea = new Area();
		sendArea.setProvince(sendAreaArray[0]);
		sendArea.setCity(sendAreaArray[1]);
		sendArea.setDistrict(sendAreaArray[2]);

		String[] recAreaArray = recAreaInfo.split("/");
		Area recArea = new Area();
		recArea.setProvince(recAreaArray[0]);
		recArea.setCity(recAreaArray[1]);
		recArea.setDistrict(recAreaArray[2]);

		model.setSendArea(sendArea);
		model.setRecArea(recArea);

		// 关联客户
		Customer customer = (Customer) ServletActionContext.getRequest()
				.getSession().getAttribute("customer");
		model.setCustomer_id(customer.getId());

		// 调用webservice，将输入传bos_management系统
		WebClient
				.create(Constant.BOS_MANAGEMENT_URL
						+ "/services/orderService/order/save")
				.type(MediaType.APPLICATION_JSON).post(model);

		return SUCCESS;
	}
}
